/**
 * 
 */

$(function(){
	//添加角色
	$("#addRoleBtn").click(function(){
		$.layer({
		    type: 2,
		    shade: [0],
		    fix: false,
		    title: '添加角色',
		    maxmin: false,
		    iframe: {src : global.rootPath + "/admin/role/add?type=init"},
		    area: ['400px' , '300px'],
		    close: function(index){
		    }
		});
		
	});
	
	
});

/**
 * 修改角色
 * @param id
 */
function update(id) {
	$.layer({
	    type: 2,
	    shade: [0],
	    fix: false,
	    title: '修改角色',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/admin/role/update?type=init&id="+id},
	    area: ['400px' , '300px'],
	    close: function(index){
	    }
	});
}

/**
 * 角色授权
 * @param id
 */
function authorize(id) {
	$.layer({
	    type: 2,
	    shade: [0],
	    fix: false,
	    title: '角色授权',
	    maxmin: false,
	    iframe: {src : global.rootPath + "/admin/role/auth?type=init&id="+id,scrolling: 'auto'},
	    area: ['300px' , '430px'],
	    close: function(index){
	    }
	});
}

//获取所有选中节点的值
function GetCheckedAll() {
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
    var nodes = treeObj.getCheckedNodes(true);
    
    if(nodes.length <= 0) {
    	parent.layer.alert("必须选择一个功能点", 8);
    } else {
    	var nodesId = "";
    	for (var i = 0; i < nodes.length; i++) {
    		nodesId = nodesId + (nodes[i].id) + ",";
        }
    	var id = $("#id").val();
    	
    	security.ajax({
    		url : global.rootPath + "/admin/role/auth?type=saveAuth",
    		data : {
    			id : id,
    			perIds : nodesId
    		},
    		beforeSend : function(){
    			security.showLoading();
    		},
    		success : function(code,msg) {
    			parent.layer.msg(msg, 2);
				refresh();
    			var index = parent.layer.getFrameIndex(window.name);
    			parent.layer.close(index);
    		},
    		error : function(message) {
    			security.showError(message);
    		}
    	});
    	
    }
    
    
} 

/**
 * 删除角色
 * @param id
 */
function deleteRole(id) {
	security.ajax({
		url : global.rootPath + "/admin/role/update",
		data : {
			type : "delete",
			id : id
		},
		beforeSend : function(){
			security.showLoading();
		},
		success : function(code,msg) {
			security.showSuccess(msg,refresh);
		},
		error : function(message) {
			security.showError(message);
		}
	});
}

/**
 * 刷新
 */
function refresh() {
	var formSerialize = $("#form").serializeObject();
	var v = JSON.stringify(formSerialize);
	$("#v").val(encodeURIComponent(v));
	form.submit();
}